<template>
    <div class="">
        <!-- 表格 -->
        <el-table border style="width: 100%" :data="userlist" default-expand-al>
            <el-table-column label="用户编号" width="80" prop="uid">
            </el-table-column>
            <el-table-column label="用户名称" width="200" prop="username">
            </el-table-column>
            <el-table-column label="所属角色" prop="rolename">
            </el-table-column>
            <el-table-column label="状态" prop="status">
                <template slot-scope="item">
                    <div v-if="item.row.status == 1">
                        <el-button type="success" plain disabled>启用</el-button>
                    </div>
                    <div v-else>
                        <el-button type="danger" plain disabled>禁用</el-button>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="item">
                    <div>
                        <el-button size="mini" type="primary" icon="el-icon-edit"
                            @click="edit(item.row.uid)">编辑</el-button>
                        <el-button size="mini" type="danger" icon="el-icon-delete"
                            @click="del(item.row.uid)">删除</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination @current-change="changePage" background :page-size="pageSize" layout="prev, pager, next"
            :total="count">
        </el-pagination>
    </div>
</template>
<script>
import { userDel } from '../../request/api'
import { mapGetters, mapActions } from 'vuex'
export default {
    data() {
        return {

        }
    },
    computed: {
        ...mapGetters({
            userlist: 'user/getUser',
            pageSize:'user/getSize',
            count:'user/getCount'
        })
    },
    mounted() {
        // getUserList().then(res => {
        //     console.log(res, '============');
        // })
        // console.log(this.$store);
        //  console.log(this.userlist);
        this.getUserAction();
        this.getCount()
    },
    methods: {
        edit(id) {
            this.$emit('edit', id)
        },
        ...mapActions({
            getUserAction: 'user/getUserAction',
            getCount:'user/getCountAction',
            changePageAction:'user/changePageAction'
        }),
        // 封装一个切换页面的事件
        changePage(n){
            this.changePageAction(n)
        },
        del(uid) {
            this.$confirm('你确定要删除这条数据吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                // 调用删除接口
                userDel({ uid }).then(res => {
                    console.log(res, '删除');
                    if (res.code == 200) {
                        this.$message.success(res.msg)
                        // 重新触发
                        this.getUserAction()
                        // 重新调用总数
                        this.getCount()
                    } else {
                        this.$message.error(res.msg)
                    }
                })
                //   this.$message({
                //     type: 'success',
                //     message: '删除成功!'
                //   });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        }
    }
}
</script>
<style scoped>
.btnInfo {
    margin: 10px 5px;
}
</style>